package com.mf.springboot.backend.userAuth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mf.springboot.backend.commonModule.bean.JSONResult;
import com.mf.springboot.backend.commonModule.bean.PageBean;
import com.mf.springboot.backend.commonModule.common.TextHelper;
import com.mf.springboot.backend.commonModule.common.UUIDGeneratorUtil;
import com.mf.springboot.backend.commonModule.utils.EncryptUtils;

import com.mf.springboot.backend.mybatisModule.entity.TLoginUser;
import com.mf.springboot.backend.mybatisModule.entity.TLoginUserExample;
import com.mf.springboot.backend.mybatisModule.entity.TTestUser;
import com.mf.springboot.backend.mybatisModule.entity.TTestUserExample;
import com.mf.springboot.backend.mybatisModule.mapper.TLoginUserMapper;
import com.mf.springboot.backend.mybatisModule.mapper.TTestUserMapper;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import com.xiaoleilu.hutool.crypto.digest.Digester;
import com.xiaoleilu.hutool.crypto.symmetric.AES;
import com.xiaoleilu.hutool.crypto.symmetric.DES;
import com.xiaoleilu.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lx on 2017/3/28.
 */
@Service
public class LoginService {
    @Resource
    private TLoginUserMapper loginUserMapper;

    @Resource
    private TTestUserMapper tTestUserMapper;

    @Resource
    private TestUserService testUserService;

    @Transactional
    public List test() throws RuntimeException{
        List list = new ArrayList();


        TLoginUserExample example = new TLoginUserExample();
        list=loginUserMapper.selectByExample(example);
        System.out.println(list.size());

        TLoginUser tLoginUser = new TLoginUser();
        tLoginUser.setId(UUIDGeneratorUtil.generate());
        tLoginUser.setPassword("888888");
        tLoginUser.setCreateTime(new Date().toString());
        insertLoginUser(tLoginUser);

        TTestUser tTestUser = new TTestUser();
        tTestUser.setId(UUIDGeneratorUtil.generate());
        tTestUserMapper.insertSelective(tTestUser);

        List testList = new ArrayList();
        TTestUserExample tTestUserExample = new TTestUserExample();
        testList = testUserService.selectByExample(tTestUserExample);

        return testList;
    }

    public List noTranscation(){
        List list = new ArrayList();


        TLoginUserExample example = new TLoginUserExample();
        list=loginUserMapper.selectByExample(example);
        System.out.println(list.size());

        TLoginUser tLoginUser = new TLoginUser();
        tLoginUser.setId(UUIDGeneratorUtil.generate());
        tLoginUser.setPassword("888888");
        tLoginUser.setCreateTime(new Date().toString());
        insertLoginUser(tLoginUser);

        TTestUser tTestUser = new TTestUser();
        tTestUser.setId(UUIDGeneratorUtil.generate());
        tTestUserMapper.insertSelective(tTestUser);

        List testList = new ArrayList();
        TTestUserExample tTestUserExample = new TTestUserExample();
        testList = testUserService.selectByExample(tTestUserExample);

        return testList;
    }

    public List<TTestUser> selectByExampleForTestUser(TTestUserExample example){
        return tTestUserMapper.selectByExample(example);
    }

    public List<TLoginUser> selectByExample(TLoginUserExample example){
        return loginUserMapper.selectByExample(example);
    }

    public int insertLoginUser(TLoginUser record){
        record.setPassword(EncryptUtils.encryptAes(record.getPassword()));
        return loginUserMapper.insert(record);
    }
    public TLoginUser selectByUserName(String userName){
        TLoginUserExample example = new TLoginUserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        List<TLoginUser> list = loginUserMapper.selectByExample(example);
        if (list.size()>0){
            TLoginUser loginUser = list.get(0);
            return loginUser;
        }
        return null;
    }

    public TLoginUser getById(String id){
        TLoginUser loginUser = loginUserMapper.selectByPrimaryKey(id);
        return loginUser;
    }
    public PageInfo<TLoginUser> getUserList(PageBean pageBean,TLoginUser loginUser){
        PageHelper.startPage(pageBean.getPage(),pageBean.getPageSize());
        TLoginUserExample example = new TLoginUserExample();
        TLoginUserExample.Criteria criteria = example.createCriteria();
        if(TextHelper.isNotNull(loginUser.getUserName())){
            criteria.andUserNameLike("%"+loginUser.getUserName()+"%");
        }
        List<TLoginUser>list = loginUserMapper.selectByExample(example);
        PageInfo<TLoginUser>pageInfo = new PageInfo<TLoginUser>(list);
        return pageInfo;
    }
    public JSONResult updateUser(TLoginUser loginUser){
        JSONResult result = JSONResult.getSuccess();
        try {
            loginUserMapper.updateByPrimaryKey(loginUser);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.getFail("系统错误");
        }
    }

}
